import 'package:chathouse/constant/my_color.dart';
import 'package:chathouse/util/base_utils.dart';
import 'package:chathouse/util/font_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../model/pre_question_bean.dart';
import 'form_widgets.dart';

typedef QuestionDialogWidgetSureCallback = Function(List<String> code);

class QuestionDialogWidget extends StatefulWidget {
  PreQuestionBean questionBean;
  QuestionDialogWidgetSureCallback sureCallback;
  QuestionDialogWidget(this.questionBean, this.sureCallback, {Key? key})
      : super(key: key);

  @override
  State<QuestionDialogWidget> createState() => _QuestionDialogWidgetState();
}

class _QuestionDialogWidgetState extends State<QuestionDialogWidget> {
  List<bool> arraySelected = [];

  @override
  void initState() {
    arraySelected = widget.questionBean?.list?.map((e) {
          return false;
        }).toList() ??
        [];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    double listviewHeight = (widget.questionBean.list?.length ?? 0) * 40;
    return Container(
      color: const Color.fromRGBO(0, 0, 0, 0.65),
      child: Column(mainAxisAlignment: MainAxisAlignment.end, children: [
        const Spacer(),
        Container(
          padding:
              EdgeInsets.only(left: 40.w, right: 40.w, top: 22.w, bottom: 12.w),
          margin: EdgeInsets.only(left: 30.w, right: 30.w, bottom: 120.w),
          decoration: BoxDecoration(
              color: const Color.fromRGBO(239, 254, 243, 1),
              borderRadius: BorderRadius.circular(22.w)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                widget.questionBean.problemName ?? "",
                style: getTextStyle(18.sp,
                    color: Colors.black, fontType: FontType.MID),
              ).setContainer(margin: EdgeInsets.only(bottom: 8.w)),
              ListView(
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: widget.questionBean.list?.map((e) {
                          int index = widget.questionBean.list!.indexOf(e);

                          return getItem(
                                  e.optionValue ?? "", arraySelected[index])
                              .setOnClickListener(() {
                            setState(() {
                              arraySelected[index] = !arraySelected[index];
                            });
                          });
                        }).toList() ??
                        [],
                  )
                ],
              ).setContainer(
                  constraints: BoxConstraints(
                      maxHeight: listviewHeight > 1.sh / 2.0 - 100.w
                          ? 1.sh / 2.0 - 100.w
                          : listviewHeight)),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    margin: EdgeInsets.only(top: 12.w),
                    padding: EdgeInsets.only(
                        left: 68.w, right: 68.w, top: 7.w, bottom: 7.w),
                    decoration: BoxDecoration(
                        color: const Color.fromRGBO(241, 184, 96, 1),
                        borderRadius: BorderRadius.circular(19.w)),
                    child: Text(
                      "确定",
                      style: getTextStyle(18.sp,
                          color: Colors.white, fontType: FontType.MID),
                    ),
                  ).setOnClickListener(() {
                    bool isSelected = arraySelected.firstWhere((element) {
                      return element;
                    }, orElse: () {
                      return false;
                    });
                    if (!isSelected) {
                      debugPrint("no Selected");
                      return;
                    }
                    List<String> codes = [];
                    widget.questionBean.list?.forEach((element) {
                      int i = widget.questionBean.list!.indexOf(element);
                      if (arraySelected[i]) {
                        codes.add("${element.optionCode}");
                      }
                    });
                    widget.sureCallback(codes);
                  })
                ],
              )
            ],
          ),
        )
      ]),
    );
  }

  Widget getItem(String title, bool selected) {
    return Container(
      margin: EdgeInsets.only(top: 7.w),
      width: double.infinity,
      alignment: Alignment.center,
      padding: EdgeInsets.only(top: 7.w, bottom: 7.w),
      decoration: BoxDecoration(
          border: Border.all(color: Colors.grey, width: 1.w),
          borderRadius: BorderRadius.circular(100),
          color:
              selected ? const Color.fromRGBO(212, 242, 173, 1) : Colors.white),
      child: Text(
        title,
        style: getTextStyle(16.sp, color: Colors.black),
      ),
    );
  }
}
